﻿<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Dreamer CMS - 后台管理系统</title> 
    <link href="/resource/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
    <link href="/resource/css/font-awesome.min.css" rel="stylesheet" type="text/css" />
    <link href="/resource/css/ionicons.min.css" rel="stylesheet" type="text/css" />
    <link href="/resource/css/iCheck/all.css" rel="stylesheet" type="text/css" />
    <link href="/resource/css/style.css" rel="stylesheet" type="text/css" />
    <link rel="stylesheet" type="text/css" href="/resource/js/plugins/ztree-v3/css/bootstrapZTreeStyle.css"/>
</head>

<body>
<div class="row">
    <div class="col-md-12">
    	<h1 class="panel-heading">分配权限</h1>
        <!--breadcrumbs start -->
        <ul class="breadcrumb">
			<li><a href="/admin/dashboard/toIndex"><i class="fa fa-home"></i> 首页</a></li>
			<li class="active">分配权限</li>
		</ul>
        <!--breadcrumbs end -->
    </div>
</div>
<section class="panel">
	<div class="panel-heading">
        <h3 class="panel-title">
            <b>当前角色：<span th:text="${role.roleName}"></span></b>
        </h3>
    </div>
    <div class="panel-body">
		<ul id="tree" class="ztree"></ul>		        	
        <div class="form-btn-group-left" shiro:hasAnyPermissions="system:role:togrant,system:role:grant">
        	<button type="button" onclick="grant();" class="btn btn-info" id="save">分配权限</button>
        </div>
    </div>
</section>
<!-- jQuery 2.0.2 -->
<script src="/resource/js/jquery.min.js"></script>
<!-- iCheck -->
<script src="/resource/js/plugins/iCheck/icheck.min.js"></script>
<script src="/resource/js/plugins/ztree-v3/jquery.ztree.core.min.js"></script>
<script src="/resource/js/plugins/ztree-v3/jquery.ztree.excheck.min.js"></script>
<script src="/resource/js/coco-message/coco-message.js"></script>
<script th:inline="javascript">
	var data = [[${permissions}]];
	var currentRoleId = [[${role.id}]];
	var zTree = null;
	window.onload = function(){
		var setting = {
			check: {
				enable: true
			},
	        data: {
	        	key: {
	        		checked: "checked",
	        		name: "permissionName"
	        	},
	            simpleData: {
	                enable: true,
	                idKey: "id",
	                pIdKey: "parentId",
	                rootPId: "-1"
	            }
	        }
	    };
	    zTree = $.fn.zTree.init($("#tree"), setting, data);
	    zTree.expandAll(true);
	}

	/**
	 * 授权
	 */
	function grant(){
		var checkedNodes = zTree.getCheckedNodes();
		var rolePermissions = new Array();
		for(var i = 0;i < checkedNodes.length;i++){
			var type = 1;
			if(checkedNodes[i].type == "menu"){
				type = 1;
			}else{
				type = 2;
			}
			rolePermissions.push({roleId: currentRoleId, permissionId: checkedNodes[i].id, type: type});
		}
		
		$.ajax({
			url : "/admin/role/grant/" + currentRoleId,
			contentType : 'application/json',
			data : JSON.stringify(rolePermissions),
			type : "POST",
			dataType : "JSON",
			success : function(result) {
				if(result.success){
					cocoMessage.success(result.info);
					setTimeout(function() {
						//登录返回
						window.location.href = "/admin/role/toGrant?id=" + currentRoleId;
					}, 500);
				}else{
					cocoMessage.error(result.info);
				}
			},
			error: function(err){
				if(err.status == 403){
					window.location.href = "/exception/403"
				}
			}
		});
		return false;
	}
</script>
</body>
</html>
